﻿using HlELock.Core;
using HlELock.Core.Models;
using HlELock.Domain.Entities;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;

namespace HlELock.Core
{
    public static class HttpHelper
    {
        private readonly static HttpClient _httpClient;
        static HttpHelper()
        {
            _httpClient = new HttpClient();
            _httpClient.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", MyAppContext.AccessToken);
#if DEBUG
            var host = "http://60.204.173.108:10278";

#else
            var host = ConfigurationManager.AppSettings["host"];
#endif
            _httpClient.BaseAddress = new System.Uri(host);
        }
        public static async Task<List<HotelInfo>> GetHotelsAsync()
        {
            var content = new StringContent(JsonConvert.SerializeObject(null), Encoding.UTF8, "application/json");
            var response = await _httpClient.GetAsync("/client/hotels");
            if (response.IsSuccessStatusCode)
            {
                var json = await response.Content.ReadAsStringAsync();
                var result = JsonConvert.DeserializeObject<List<HotelInfo>>(json);
                return result;
            }
            return new List<HotelInfo>();

        }

        public static async Task<HotelDeviceActiveResponse> GetHotelDeviceActiveCodeAsync()
        {
            var response = await _httpClient.GetAsync("/client/ActiveHotelDevice?code=" + MyAppContext.HotelInfo.HotelCode);
            if (response.IsSuccessStatusCode)
            {
                var json = await response.Content.ReadAsStringAsync();
                var result = JsonConvert.DeserializeObject<HotelDeviceActiveResponse>(json);
                return result;
            }
            else
            {
                return null;
            }
        }
    }

    public class HotelDeviceActiveResponse
    {
        public string HotelCode { get; set; }
        public string AgencyCode { get; set; }
        public string AreaCode { get; set; }

        public bool IsActive
        {
            get => !string.IsNullOrWhiteSpace(HotelCode) && !string.IsNullOrWhiteSpace(AgencyCode) && !string.IsNullOrWhiteSpace(AreaCode) && AreaCode.Length == 8;
        }
    }
}
